﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mail;

namespace Ccit.Utils
{
    /// <summary>
    /// 电子邮件类
    /// </summary>
    public class EmailLib
    {
        #region private attributes
        private string _senderMail;
        private string _receiveMails;
        private string _subject;
        private MailPriority _priority;
        private MailFormat _mailFormat;
        private string _content;
        private List<string> _attachList;

        private string _account;  //发送方账号
        private string _password; //邮箱口令
        private string _smtpServer;  //SMTP邮件服务器
        #endregion

        #region public attributes
        /// <summary>
        /// 邮件发送方email 地址
        /// </summary>
        public string SenderEmail
        {
            get { return ((!string.IsNullOrEmpty(_senderMail)) ? _senderMail : ""); }
            set { _senderMail = value; }
        }

        /// <summary>
        /// 邮件接收方email 地址(多个地址间以分号分隔)
        /// </summary>
        public string ReceiveEmails
        {
            get { return ((!string.IsNullOrEmpty(_receiveMails)) ? _receiveMails : ""); }
            set { _receiveMails = value; }
        }

        /// <summary>
        /// 邮件主题
        /// </summary>
        public string MailSubject
        {
            get { return ((!string.IsNullOrEmpty(_subject)) ? _subject : ""); }
            set { _subject = value; }
        }

        /// <summary>
        /// 邮件发送优先级
        /// </summary>
        public MailPriority Priority
        {            
            set { _priority = value; }
        }

        /// <summary>
        /// 邮件内容格式
        /// </summary>
        public MailFormat MailFormat
        {            
            set { _mailFormat = value; }
        }

        /// <summary>
        /// 邮件内容
        /// </summary>
        public string MailContent
        {
            get { return ((!string.IsNullOrEmpty(_content)) ? _content : ""); }
            set { _content = value; }
        }

        /// <summary>
        /// 邮件附件地址列表
        /// </summary>
        public List<string> AttachList
        {
            get { return _attachList; }
            set { _attachList = value; }
        }

        /// <summary>
        /// 发送方邮箱账号
        /// </summary>
        public string Account
        {
            set { _account = value; }
        }

        /// <summary>
        /// 发送方邮箱登陆口令
        /// </summary>
        public string Password
        {
            set { _password = value; }
        }

        /// <summary>
        /// SMTP 邮件服务器
        /// </summary>
        public string SMTPServer
        {
            set { _smtpServer = value; }
        }
        #endregion

        #region constructor
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public EmailLib() { }

        /// <summary>
        /// 不带附件，使用默认优先级发送的纯文本邮件
        /// </summary>
        /// <param name="sender">发送方邮箱地址</param>
        /// <param name="account">发送方邮箱账号</param>
        /// <param name="password">发送方邮箱登陆口令</param>
        /// <param name="receivers">接收方邮箱地址(多个邮箱以分号隔开)</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="content">邮件内容</param>
        /// <param name="smtpServer">SMTP邮件服务器</param>
        public EmailLib(string sender,string account,string password, string receivers, string subject, string content,string smtpServer)
        {
            this._senderMail = sender;
            this._receiveMails = receivers;
            this._subject = subject;
            this._priority = MailPriority.Low;
            this._mailFormat = MailFormat.Text;
            this._content = content;
            this._attachList = null;
            this._account = account;
            this._password = password;
            this._smtpServer = smtpServer;
        }

        /// <summary>
        /// 邮件构造函数(带附件)
        /// </summary>
        /// <param name="sender">发送方邮箱地址</param>
        /// <param name="account">发送方邮箱账号</param>
        /// <param name="password">发送方邮箱登陆口令</param>
        /// <param name="receivers">接收方邮箱地址(多个邮箱以分号隔开)</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="priority">邮件发送优先级</param>
        /// <param name="bodyFormat">邮件内容格式</param>
        /// <param name="content">邮件内容</param>
        /// <param name="attachList">附件地址列表</param>
        /// <param name="smtpServer">SMTP邮件服务器</param>
        public EmailLib(string sender, string account, string password, string receivers, string subject,
                                MailPriority priority, MailFormat bodyFormat, string content, List<string> attachList, string smtpServer)
        {
            this._senderMail = sender;
            this._receiveMails = receivers;
            this._subject = subject;
            this._priority = priority;
            this._mailFormat= bodyFormat;
            this._content = content;
            this._attachList = attachList;
            this._account = account;
            this._password = password;
            this._smtpServer = smtpServer;
        }
        #endregion

        #region public instance interface
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <returns>操作结果集合 [0]: 邮件发送成功/失败标志 1:成功 -999:失败
        ///                       [1]: 操作结果提示
        ///                       [2]: 错误描述
        /// </returns>
        public List<string> Send()
        {
            List<string> lstSend = new List<string>();
            try
            {
                MailMessage myMail=new MailMessage(); 
                myMail.From = _senderMail; 
                myMail.To = ReceiveEmails;
                myMail.Subject = _subject;
                myMail.Priority = _priority;   
                myMail.BodyFormat = _mailFormat; 
                myMail.Body = _content;
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",_account); 
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",_password); 
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", _smtpServer);

                MailAttachment _attach;
                if (null != _attachList && _attachList.Count > 0)
                {
                    foreach(string eAttachAddress in _attachList)
                    {
                        _attach = new MailAttachment(eAttachAddress);
                        myMail.Attachments.Add(_attach);
                    }
                }
             
             
                SmtpMail.SmtpServer = "smtp.sina.com.cn"; //SMTP服务器名称
                SmtpMail.Send(myMail);

                myMail = null;
                lstSend.Add("1");
                lstSend.Add("邮件发送成功!");
                lstSend.Add("");
             }    
             catch(Exception ex) 
             {
                 lstSend.Add("-999");
                 lstSend.Add("邮件发送失败,请检查邮箱配置是否正确,然后重新尝试发送!");
                 lstSend.Add(ex.Message);
             }

            return lstSend;
        }
        #endregion
    }
}
